<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    table {
      border: 1px solid #e9e9e9;
      border-collapse: collapse;
    }

    td, th {
      border: 1px solid #e9e9e9;
      padding: 8px 16px;
    }

    th {
      background-color: #f7f7f7;
      color: #5c6b77;
    }

    .counter {
      margin: 0 5px;
    }
  </style>
</head>
<body>
  <div id="app"></div>
  <!-- 1.引入js文件 -->
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
  <script src="./formatPrice.js"></script>
  <script type="text/babel">
    class App extends React.Component {
      constructor(props) {
        super(props)
        this.state = {
          books: [
            {
              id: 1,
              name: '《算法导论》',
              date: '2006-9',
              price: 85.00,
              count: 1
            },
            {
              id: 2,
              name: '《UNIX编程艺术》',
              date: '2006-2',
              price: 59.00,
              count: 1
            },
            {
              id: 3,
              name: '《编程珠玑》',
              date: '2008-10',
              price: 39.00,
              count: 1
            },
            {
              id: 4,
              name: '《代码大全》',
              date: '2006-3',
              price: 128.00,
              count: 1
            },
          ]
        }
      }
      render() {
        const { books } = this.state;
        return books.length ? this.renderBooks() : this.renderEmpty()
      }
      getTotalPrice() {
        let totalPrice = 0;
        for (let book of this.state.books) {
          totalPrice += book.count * book.price
        }
        return "总价格：" + formatPrice(totalPrice);
      }
      changeItem(index, counter) {
        const books = [...this.state.books]
        this.setState({
          books: books.map((item, index2) => {
            if (index2 === index) {
              item.count += counter
            }
            return item
          })
        })
      }
      removeItem(index) {
        const books = [...this.state.books]
        this.setState({
          books: books.filter((item, index2)=> index !== index2)
        })
      }
      renderEmpty() {
        return <h2>购物车为空~</h2>
      }
      renderBooks() {
        const { books } = this.state
        return (
          <div>
            <table>
              <thead>
                <tr>
                  <th></th>  
                  <th>书籍名称</th>  
                  <th>出版日期</th>  
                  <th>价格</th>  
                  <th>购买数量</th>  
                  <th>操作</th>  
                </tr>  
              </thead>
              <tbody>
                {
                  books.map((item, index) => {
                    return (
                      <tr>
                        <td>{index + 1}</td>
                        <td>{item.name}</td>
                        <td>{item.date}</td>
                        <td>{formatPrice(item.price)}</td>
                        <td>
                          <button disabled={item.count <= 1} onClick={e => this.changeItem(index, -1)}>-</button>
                          <span className="counter">{item.count}</span>
                          <button onClick={e => this.changeItem(index, 1)}>+</button>
                        </td>
                        <td><button onClick={e=>this.removeItem(index)}>移除</button></td>
                      </tr>
                    )
                  })
                }
              </tbody>  
            </table>
            <h2>
              {this.getTotalPrice()}
            </h2>
          </div>
        )  
      }
    }
    ReactDOM.render(<App/>, document.getElementById("app"));
  </script>
</body>
</html>